package edu.uwosh.cs342.group2.project4.database;

import java.util.*;

import edu.uwosh.cs342.group2.project4.storage.Category;
import edu.uwosh.cs342.group2.project4.storage.Coordinates;
import edu.uwosh.cs342.group2.project4.storage.Location;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.widget.Toast;

public class LocationDb extends ObjectDb {
	
	/**
	 * Default constructor for Locations
	 * @param context
	 */
	protected LocationDb(Context context) {
		super(context);
	}
	
	/**
	 * Adds a location to the database
	 * @param location
	 * @return -1 if failed, > 0 of the correct ID
	 */
	public int AddLocation(Location location) {
		int locationId = 0;
		ContentValues args = new ContentValues();
		
		args.put("Name", location.getName().trim());
		args.put("Description", location.getDescription().trim());
		args.put("Longitude", location.getCoordinates().getLongitude());
		args.put("Latitude", location.getCoordinates().getLatitude());
		args.put("Location", location.getLocation().trim());
		args.put("CategoryId", location.getCategory().getId());
		try {
			locationId = (int)db.insert("Location", null, args);
		}
		catch (SQLiteException e) {
			Toast.makeText(this.context, "Could not add the location", 25).show();
			locationId = -1;
		}
		finally {
			this.CloseConnection();
		}
		
		return locationId;
	}
	
	/**
	 * Gets a location's information
	 * @param id
	 * @return
	 */
	public Location GetLocation(int id) {
		String[] columns= {
			"Id",
			"Name",
			"Description",
			"Longitude",
			"Latitude",
			"Location",
			"CategoryId"
		};
		Location location = new Location();
		try {
			Cursor set = db.query("Location", columns, "Id = " + String.valueOf(id), null, null, null, null);
			if (set.moveToNext()) {
				location.setId(set.getInt(set.getColumnIndex("Id")));
				location.setName(set.getString(set.getColumnIndex("Name")));
				location.setDescription(set.getString(set.getColumnIndex("Description")));
				Coordinates coordinates = new Coordinates(
					set.getDouble(set.getColumnIndex("Latitude")),
					set.getDouble(set.getColumnIndex("Longitude"))
				);
				location.setCoordinates(coordinates);
				location.setLocation(set.getString(set.getColumnIndex("Location")));
				location.setCategory(new Category(set.getInt(set.getColumnIndex("CategoryId"))));
			}
		}
		catch (SQLiteException ex) {
			Toast.makeText(this.context, "Could not get the location information", 25).show();
			location = null;
		}
		finally {
			this.CloseConnection();
		}
		
		return location;
	}
	
	/**
	 * Gets all of the saved locations
	 * @return
	 */
	public ArrayList<Location> GetAllLocations() {
		String[] columns= {
				"Id",
				"Name",
				"Description",
				"Longitude",
				"Latitude",
				"Location",
				"CategoryId"
			};
			ArrayList<Location> locationList = new ArrayList<Location>();
			try {
				Cursor set = db.query("Location", columns, null, null, null, null, null);
				while (set.moveToNext()) {
					Location location = new Location();
					location.setId(set.getInt(set.getColumnIndex("Id")));
					location.setName(set.getString(set.getColumnIndex("Name")));
					location.setDescription(set.getString(set.getColumnIndex("Description")));
					Coordinates coordinates = new Coordinates(
						set.getDouble(set.getColumnIndex("Latitude")),
						set.getDouble(set.getColumnIndex("Longitude"))
					);
					location.setCoordinates(coordinates);
					location.setLocation(set.getString(set.getColumnIndex("Location")));
					location.setCategory(new Category(set.getInt(set.getColumnIndex("CategoryId"))));
					locationList.add(location);
				}
			}
			catch (SQLiteException ex) {
				Toast.makeText(this.context, "Could not get the location information", 25).show();
				locationList = null;
			}
			finally {
				this.CloseConnection();
			}
			
			return locationList;
	}
	
	/**
	 * Searches for locations by name and/or the distance (in miles)
	 * @param value
	 * @param distance
	 * @return
	 */
	public ArrayList<Location> SearchLocations(String value, int distance) {
		return new ArrayList<Location>();
	}
	
	/**
	 * Updates a location from the list
	 * @param location
	 * @return
	 */
	public boolean UpdateLocation(Location location) {
		ContentValues args = new ContentValues();
		args.put("Name", location.getName().trim());
		args.put("Description", location.getDescription().trim());
		args.put("Longitude", location.getCoordinates().getCords().getLongitudeE6());
		args.put("Latitude", location.getCoordinates().getCords().getLatitudeE6());
		args.put("Location", location.getLocation().trim());
		args.put("CategoryId", location.getCategory().getId());
		try {
			db.update("Location", args, "Id = " + location.getId(), null);
		}
		catch (SQLiteException e) {
			Toast.makeText(this.context, "Could not update the location information.", 25).show();
			return false;
		}
		finally {
			this.CloseConnection();
		}
		
		return true;
	}
	
	/**
	 * Removes a location from the list
	 * @param id
	 * @return
	 */
	public boolean RemoveLocation(int id) {
		try {
			db.delete("Location", "Id = " + id, null);
		}
		catch (SQLiteException e) {
			Toast.makeText(this.context, "Could not remove the location", 25).show();
			return false;
		}
		finally {
			this.CloseConnection();
		}
		
		return true;
	}
}
